﻿using System.Diagnostics;
using System.Windows;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    public static class SpreadsheetBrushes
    {
        private static bool initialized;

        public static ResourceKey WorksheetButtonBrush1
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.WorksheetButtonBrush1); }
        }

        public static ResourceKey WorksheetButtonBrush2
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.WorksheetButtonBrush2); }
        }

        public static ResourceKey WorksheetButtonBrush3
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.WorksheetButtonBrush3); }
        }

        public static ResourceKey WorksheetButtonBrush4
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.WorksheetButtonBrush4); }
        }

        public static ResourceKey SheetBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.SheetBackground); }
        }

        public static ResourceKey SheetTabBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.SheetTabBorderBrush); }
        }

        public static ResourceKey SheetTabBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.SheetTabBackground); }
        }

        public static ResourceKey SheetTabSelectedBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.SheetTabSelectedBackground); }
        }

        public static ResourceKey GroupBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.GroupBackground); }
        }

        public static ResourceKey GroupLightBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.GroupLightBackground); }
        }

        public static ResourceKey RowBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowBorderBrush); }
        }

        public static ResourceKey ColumnBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnBorderBrush); }
        }

        public static ResourceKey SelectionBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.SelectionBackground); }
        }

        public static ResourceKey ColumnBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnBackground); }
        }

        public static ResourceKey ColumnSelectedBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnSelectedBackground); }
        }

        public static ResourceKey ColumnSelectedBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnSelectedBorderBrush); }
        }

        public static ResourceKey ColumnHoverBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnHoverBackground); }
        }

        public static ResourceKey ColumnLightBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnLightBorderBrush); }
        }

        public static ResourceKey ColumnSelectedHoverBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnSelectedHoverBackground); }
        }

        public static ResourceKey ColumnFullSelectedBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.ColumnFullSelectedBackground); }
        }

        public static ResourceKey RowFullSelectedBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowFullSelectedBackground); }
        }

        public static ResourceKey RowBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowBackground); }
        }

        public static ResourceKey RowSelectedBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowSelectedBackground); }
        }

        public static ResourceKey RowLightBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowLightBorderBrush); }
        }

        public static ResourceKey RowSelectedBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowSelectedBorderBrush); }
        }

        public static ResourceKey RowHoverBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowHoverBackground); }
        }

        public static ResourceKey RowSelectedHoverBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.RowSelectedHoverBackground); }
        }

        public static ResourceKey FormulaBackgroundKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.FormulaBackground); }
        }

        public static ResourceKey FormulaBorderBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.FormulaBorderBrush); }
        }

        public static ResourceKey GridLineBrushKey
        {
            get { return GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId.GridLineBrush); }
        }

        private static ResourceKey GetSpreadsheetUIResourceKey(SpreadsheetUIResourceId id)
        {
            var result = new SpreadsheetUIResourceKey(id);
            if (!initialized)
            {
                initialized = true;
                Application app = Application.Current;
                if (app != null)
                {
                    foreach (ResourceDictionary dictionary in app.Resources.MergedDictionaries)
                    {
                        if (dictionary.GetType().GetCustomAttributes(typeof (SpreadsheetSkinAttribute), true).Length > 0)
                            return result;
                    }

                    foreach (
                        ResourceDictionary dictionary in
                            Enumerable.ToList(Enumerable.OfType<Office2007BlueSkin>(app.Resources.MergedDictionaries)))
                        app.Resources.MergedDictionaries.Remove(dictionary);
                    Application.Current.Resources.MergedDictionaries.Add(new Spreadsheet2007BlueSkin());
                }
            }
        
            return result;
        }
    }
}